package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.Leave;
import com.tongzhan.service.LeaveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Resource
    private LeaveService leaveService;

    /**
     * 请销假申请接口
     *
     * @param leaveCode 请销假编码
     * @param userId 用户id
     * @param leaveName 请假人姓名
     * @param startTime 请假开始时间
     * @param endTime 请假结束时间
     * @param describe 请假事由
     * @return Return
     */
    @PostMapping("/leaveApplication")
    public Return leaveApplication(String leaveCode, String userId, String leaveName, String startTime, String endTime, String describe) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)
                || StringUtils.isEmpty(describe) || "".equals(describe.trim())
                || StringUtils.isEmpty(leaveName) || "".equals(leaveName.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (leaveName.trim().length() > 100) {
            return new Return(Return.FAIL, "请假人姓名过长");
        }
        if (describe.trim().length() > 2000) {
            return new Return(Return.FAIL, "请假事由过长");
        }
        Leave leave = new Leave();
        leave.setLeaveCode(leaveCode);
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            //开始时间
            LocalDate start = LocalDate.parse(startTime, dtf);
            //结束时间
            LocalDate end = LocalDate.parse(endTime, dtf);
            if (start.compareTo(end) > 0) {
                return new Return(Return.FAIL, "开始时间不能大于结束时间");
            }
            leave.setStartTime(start);
            leave.setEndTime(end);
            leave.setDescribe(describe.trim());
            leave.setLeaveName(leaveName.trim());
        } catch (Exception e) {
            return new Return(Return.FAIL, "日期格式不正确");
        }
        return leaveService.leaveApplication(userId, leave);
    }

    /**
     * 根据用户id获取请销假列表接口
     *
     * @param userId 用户id
     * @param religionCode 教派编码
     * @param time 年月
     * @param state 审核状态
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getLeaveList")
    public Return getLeaveList(String userId, Integer page, String religionCode, String time, String state,
                               @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId) || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return leaveService.getLeaveList(userId, page, size, religionCode, time, state);
    }

    /**
     * 请销假审核接口
     *
     * @param userId 用户id
     * @param leaveCode 请销假编码
     * @param opinion 审核意见
     * @return Return
     */
    @PostMapping("/leaveExamine")
    public Return leaveExamine(String userId, String leaveCode, String opinion) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(leaveCode) || StringUtils.isEmpty(opinion)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return leaveService.leaveExamine(userId, leaveCode, opinion);
    }

    /**
     * 场所销假接口
     *
     * @param leaveCode 请销假编码
     * @return Return
     */
    @PostMapping("/agreeLeave")
    public Return agreeLeave(String leaveCode) {
        if (StringUtils.isEmpty(leaveCode)) {
            return new Return(Return.FAIL, "参数缺失");
        }
        return leaveService.agreeLeave(leaveCode);
    }

    /**
     * 请销假删除
     *
     * @param leaveCode leaveCode
     * @return Return
     */
    @PostMapping("/deleteLeave")
    public Return deleteLeave(String leaveCode) {
        if (StringUtils.isEmpty(leaveCode)) {
            return new Return(Return.FAIL, "参数缺失");
        }
        return leaveService.deleteLeave(leaveCode);
    }
}
